package com.gjintel.business.proj.controller;

import com.gjintel.auction.business.model.AuctionInfo;
import com.gjintel.auction.business.service.IAuctionService;
import com.gjintel.business.proj.model.ComposeIdPK2;
import com.gjintel.business.proj.model.DepositReturnOut;
import com.gjintel.business.proj.service.IDepositReturnOutService;
import com.gjintel.business.proj.service.IProjectInfoService;
import com.gjintel.framework.common.service.IDepositService;
import com.gjintel.framework.constant.Constant;
import com.gjintel.framework.util.CommUtil;
import com.gjintel.sdk.base.dao.Page;
import com.gjintel.sdk.util.StringUtil;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import javax.transaction.Transactional;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.*;
/**
 * 退回系统外账号资金
 * @author ruix
 *
 */
@RequestMapping("/biz/return2")
@Controller
@SuppressWarnings("unchecked")
public class ReturnOutSysController {

	@Autowired
	private IDepositReturnOutService depositReturnOutService;
	
	@Autowired
	private IDepositService depositService;
	
	@Autowired
	private IProjectInfoService projectInfoService;

	@Autowired
	private IAuctionService auctionService;
	/**
	 * 操作页面
	 */
	@RequestMapping("/index")
	public ModelAndView index(){
		return new ModelAndView("/auction/finance/index");
	}
	
	/**
	 * 系统外用户的json数据
	 * @throws IOException 
	 */
	@RequestMapping("/list")
	public void listJson(HttpServletResponse resp,Page page, java.sql.Date transTimeStart,java.sql.Date transTimeEnd, String projectName,String companyName) throws IOException{
		// 结束时间处理，加一天
		transTimeEnd = StringUtil.addOneDayDate(transTimeEnd, 1);
		List<DepositReturnOut> drList=this.depositReturnOutService.getDepositReturnOutInfo(page,transTimeStart,transTimeEnd,projectName,companyName);
		JSONObject jsonObject = CommUtil.getPageJSONData(page, true);
		resp.getWriter().print(jsonObject);
	}
	
	
	@RequestMapping("/return")
	public void backBank(){
		
	}
	
	/**
	 * @description 退还保证金
	 * @author shy
	 * @return
	 */
	@RequestMapping("/returnDeposit")
	@ResponseBody
	@Transactional
	public String returnDeposit(ComposeIdPK2 pk) {
		String result="";
		//获取biddername;
		//bidderName=this.registerUserService.get(RegisterUser.class,bidderId).getLoginName();
		//DepositReturn depositReturn = depositReturnService.getDepositReturn(bidderName,projectId);
		AuctionInfo projectInfo = this.auctionService.get(AuctionInfo.class, pk.getProjectId());
//		ComposeIdPK2 pk=new ComposeIdPK2();
//		pk.setCompanyName(companyName);
//		pk.setHstSeqNum(hstSeqNum);
//		pk.setProjectId(projectId);
		DepositReturnOut depositReturnOut =this.projectInfoService.get(DepositReturnOut.class,pk);
		
		//设置延迟
	    final ExecutorService exec = Executors.newFixedThreadPool(1); 
	    try {
	        Future<String> future = exec.submit(new BankResult(pk));  
	        Integer bankResult =Integer.parseInt(future.get(15000, TimeUnit.MILLISECONDS)); //设置8秒
	        if(bankResult==1){
	        	depositReturnOut.setReturnOpt(1);//银行返回1 申请退还成功
				 this.depositReturnOutService.saveOrUpdate(depositReturnOut);
				 result= Constant.SAVE_SUCCESS;
	        }else if(bankResult==0){//银行返回0 申请退还失败
	        	depositReturnOut.setReturnOpt(2);
				 this.depositReturnOutService.saveOrUpdate(depositReturnOut);	
				 result= Constant.SAVE_UN_SUCCESS;
	        }
	        System.out.println("银行回调成功....");  
	    } catch (Exception e) {
	    	depositReturnOut.setReturnOpt(-1);//超时未返回
			 this.depositReturnOutService.saveOrUpdate(depositReturnOut);	
	        System.out.println("银行回调超时...."); 
	        result= Constant.BANK_TIME_OUT;
	    } 
	    exec.shutdown();  
	    return result;
 	}  
	
	class BankResult implements Callable<String> {  
	    private ComposeIdPK2 pk;
	    public BankResult(ComposeIdPK2 pk) {  
	        this.pk=pk;
	    }  
	      
	    @Override  
	    public String call() throws Exception {  
	    	int result = depositService.returnDepositOutSysByProj(null,pk.getProjectId(),pk);
	        return String.valueOf(result);  
	    }  
	}  
}
